import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    component: () => import("../pages/index.vue"),
    children: [
      {
        path: '/home',
        component: () => import("../views/home.vue"),
        meta: {
          title: '小U商城',
          active: 0
        },
      }, {
        path: '/items',
        component: () => import("../views/items.vue"),
        meta: {
          title: '二级分类',
          active: 1
        },
      }, {
        path: '/cart',
        component: () => import("../views/cart.vue"),
        meta: {
          title: '购物车',
          active: 2
        }
      }, {
        path: '/my',
        component: () => import("../views/my.vue"),
        meta: {
          title: '个人中心',
          active: 3
        },
      }, {
        path: '',
        redirect: "/home"
      },
    ]
  },
  {
    path: '/list',
    component: () => import("../pages/list.vue"),
    meta: {
      title: '商品分类'
    },
  },
  {
    path: '/detail',
    component: () => import("../pages/detail.vue"),
    meta: {
      title: '商品详情'
    },
  },
  {
    path: '/login',
    component: () => import("../pages/login.vue"),
    meta: {
      title: '登录页'
    },
  },
  {
    path: '/register',
    component: () => import("../pages/register.vue"),
    meta: {
      title: '注册页'
    },
  },
  {
    path: "/order",
    component: () => import("../pages/order.vue"),
    meta: {
      title: '确认订单'
    }
  },
  {
    path: "/address",
    component: () => import("../pages/address.vue"),
    meta: {
      title: '地址管理'
    }
  },
  {
    path: "/addressadd",
    component: () => import("../pages/addressadd.vue"),
    meta: {
      title: '添加地址'
    }
  },
  {
    path: '*',
    redirect: '/index'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (savedPosition) {
          resolve(savedPosition)
        } else {
          resolve({
            x: 0,
            y: 0
          })
        }
      }, 400)
    })
  }
})

export default router
